/**
 * <p>Title: ServiceZMCP09.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张志芳
 * @version 1.0
 * <pre>Histroy:
 *          2011-10-24 下午03:31:07  张志芳  Create
 *</pre>
 */
package com.baosight.sczx.zm.cp.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiColumn;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.StaticBlocks;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.cs.Engineering;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.common.zm.cInterface.cs.SystemSettingCP;
import com.baosight.sczx.zm.cp.domain.ZMCPProducePlan;
import com.baosight.sczx.zm.cp.domain.ZMCPProducePlanFT;
import com.baosight.sczx.zm.cp.domain.ZMCPProducePlanGLAZ;
import com.baosight.sczx.zm.cp.domain.ZMCPProducePlanZC;
import com.baosight.sczx.zm.cp.domain.ZMCPProducePlanZK;
import com.baosight.sczx.zm.cp.domain.ZMCPProfessionalPlan;

/**
 * 抽采专业计划
 * <p>
 * Title: ServiceZMCP14.java
 * </p>
 */
public class ServiceZMCP14 extends ServiceEPBase {

	/**
	 * <p>
	 * Function: initLoad
	 * </p>
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");// 如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		// 设置每月可以修改所有计划的起始日期和终止日期
		outInfo.set("fdModifyStartDate", SystemSettingCP.getInstance().getModifyStartDate());// 计划修改的起始日期
		outInfo.set("fdModifyEndDate", SystemSettingCP.getInstance().getModifyEndDate());// 计划修改的终止日期
		outInfo.set("currentDate", StaticTools.getDate());// 计划修改的终止日期
		outInfo.addBlock(StaticBlocks.getPlanMonth("cmes.cp.planMonth", true));
		outInfo.addBlock(StaticBlocks.getPlanStatus("cmes.cp.planStatus", true));
		return outInfo;
	}

	/**
	 * <p>
	 * Function: query
	 * </p>
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		StaticTools.setQueryParameters(inInfo, "fdPlanType", "抽采计划", true);
		StaticTools.setQueryParameters(inInfo, "fdMiningId", UserSession.getInSessionProperty("miningCode"), true);
		EiInfo outInfo = super.query(inInfo, "ZMCPProfessionalPlan.query", new ZMCPProfessionalPlan());
		outInfo.addBlock(Department.getInstance().getMiningDepts("cmes.cp.department", "orgCode", "orgName", false,
				UserSession.getInSessionProperty("miningCode").toString()));
		outInfo.addBlock(StaticBlocks.GetEngineeringClassTF(false));
		outInfo.addBlock(Engineering.getEngineeringDetail("cmes.cp.engineering", "2", "打钻", "1", true));
		outInfo.addBlock(getHoleType());
		outInfo.addBlock(StaticBlocks.getPlanMonth("cmes.cp.planMonth", true));
		return outInfo;
	}

	/**
	 * <p>
	 * Function: insert
	 * </p>
	 * <p>
	 * Description: 新增
	 * </p>
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) {
		EiInfo out = new EiInfo();
		ZMCPProfessionalPlan _dom = new ZMCPProfessionalPlan();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();

		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			Map<String, String> params = new HashMap<String, String>();
			params.put("fdPlanType", _dom.getFdPlanType());
			params.put("fdPlanYear", _dom.getFdPlanYear());
			params.put("fdPlanMonth", _dom.getFdPlanMonth());
			params.put("fdMiningId", UserSession.getInSessionProperty("miningCode").toString());
			boolean validate = this.validate(_dom, "ZMCPProfessionalPlan.query", params, "insert");
			if (validate) {
				out = this.query(inInfo);
				out.setMsg("该年月的抽采专业计划已经存在！");
				return out;
			} else {
				String fdCreateDepartmentId = _dom.getFdCreateDepartmentId();
				_dom.setFdCreateDepartment(Department.getInstance().getDepartmentNameById(fdCreateDepartmentId));
				_dom.setFdMiningId(UserSession.getInSessionProperty("miningCode").toString());
				_dom.setFdMiningName(UserSession.getInSessionProperty("miningName").toString());
				_dom.setFdCreator(UserSession.getInSessionProperty("UserCname").toString());
				_dom.setFdCreatorId(UserSession.getUserId().toString());
				_dom.setFdCreateDepartment(UserSession.getInSessionProperty("orgName").toString());
				_dom.setFdCreateDepartmentId(UserSession.getInSessionProperty("orgCode").toString());
				_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CP_PROFESSIONALPLAN"));
				this.dao.insert("ZMCPProfessionalPlan.insert", _dom);
				// this.dao.update("ZMCPProfessionalPlan.updateProfessionalplanYM",_dom);
			}

		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;
	}

	/**
	 * <p>
	 * Function: update
	 * </p>
	 * <p>
	 * Description: 修改
	 * </p>
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {
		EiInfo out = new EiInfo();
		ZMCPProfessionalPlan _dom = new ZMCPProfessionalPlan();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		Map<String, String> params = new HashMap<String, String>();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			params.put("fdProfessionalplanId", _dom.getFdKeyId());
			boolean validate = this.validate1(inInfo, "ZMCPProducePlanZC.query", params, "update");
			boolean validate1 = this.validate1(inInfo, "ZMCPProducePlanZK.query", params, "update");
			boolean validate2 = this.validate1(inInfo, "ZMCPProducePlanGLAZ.query", params, "update");
			if (validate || validate1 || validate2) {
				return inInfo;
			} else {
				params.put("fdPlanYear", _dom.getFdPlanYear());
				params.put("fdPlanMonth", _dom.getFdPlanMonth());
				params.put("fdPlanType", "抽采计划");
				params.put("fdMiningId", _dom.getFdMiningId());
				params.put("fdMiningName", _dom.getFdMiningName());
				params.put("fdKeyId", _dom.getFdKeyId());
				boolean validate3 = this.validate(_dom, "ZMCPProfessionalPlan.updateValidate", params, "update");
				if (validate3) {
					out = this.query(inInfo);
					out.setMsg("该年月的抽采专业计划已经存在！");
					return out;
				} else {
					_dom.setFdCreator(UserSession.getInSessionProperty("UserCname").toString());
					_dom.setFdCreatorId(UserSession.getUserId().toString());
					_dom.setFdCreateDepartment(UserSession.getInSessionProperty("orgName").toString());
					_dom.setFdCreateDepartmentId(UserSession.getInSessionProperty("orgCode").toString());
					this.dao.update("ZMCPProfessionalPlan.update", _dom);
					// this.dao.update("ZMCPProfessionalPlan.updateProfessionalplanYM",_dom);
				}
			}
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;
	}

	/**
	 * <p>
	 * Function: delete
	 * </p>
	 * <p>
	 * Description: 删除
	 * </p>
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {

		EiInfo outInfo = new EiInfo();
		ZMCPProducePlanFT _dom = new ZMCPProducePlanFT();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();

		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			// 判断是否有明细
			Map<String, String> params = new HashMap<String, String>();
			params.put("fdProfessionalplanId", _dom.getFdKeyId());
			boolean validate = this.validate1(inInfo, "ZMCPProducePlanZC.query", params, "delete");
			boolean validate1 = this.validate1(inInfo, "ZMCPProducePlanZK.query", params, "delete");
			boolean validate2 = this.validate1(inInfo, "ZMCPProducePlanGLAZ.query", params, "delete");
			if (validate || validate1 || validate2) {
				return inInfo;
			} else {

				this.dao.delete("ZMCPProfessionalPlan.delete", _dom);
			}

		}
		outInfo = this.query(inInfo);
		outInfo.setStatus(1);
		outInfo.setMsg("操作" + rowcount + "记录成功");
		return outInfo;
	}

	/**
	 * <p>
	 * Function: report
	 * </p>
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public EiInfo report(EiInfo inInfo) {
		EiInfo produceInfo = new EiInfo();
		produceInfo.addBlock("result").addBlockMeta(new ZMCPProducePlan().eiMetadata);
		produceInfo.addBlock("updateResult").addBlockMeta(new ZMCPProducePlan().eiMetadata);
		int j = 0, h = 0;
		for (int i = 0; i < inInfo.getBlock("result").getRowCount(); i++) {
			String fdPlanYear = inInfo.getBlock("result").getCellStr(i, "fdPlanYear");
			String fdPlanMonth = inInfo.getBlock("result").getCellStr(i, "fdPlanMonth");

			// 判断是否有明细
			Map map = new HashMap();
			map.put("fdProfessionalplanId", inInfo.getBlock("result").getCellStr(i, "fdKeyId"));
			List<ZMCPProducePlanZC> list_0 = dao.query("ZMCPProducePlanZC.query", map);
			List<ZMCPProducePlanZK> list_1 = dao.query("ZMCPProducePlanZK.query", map);
			List<ZMCPProducePlanGLAZ> list_2 = dao.query("ZMCPProducePlanGLAZ.query", map);

			if (list_0.size() > 0 && list_1.size() > 0 && list_2.size() > 0) {
				// 查询是否已经有当月的草拟或生效的生产计划主表
				Map<String, String> params = new HashMap<String, String>();
				params.put("fdPlanYear", fdPlanYear);
				params.put("fdPlanMonth", fdPlanMonth);
				List<ZMCPProducePlan> _result = dao.query("ZMCPProducePlan.query", params);

				String fdProduceId = TablePk.getInstance().getNextValue("T_CP_PRODUCEPLAN");
				// 如果生产计划主表记录不存在，则生成一条记录
				if (_result != null && _result.isEmpty()) {
					// 查询年计划分解量
					Map map1 = new HashMap();
					map1.put("fdPlanYear", fdPlanYear);
					map1.put("fdPlanStatus", "生效");
					List<ZMCPProducePlan> list1 = dao.query("ZMCPProducePlan.query", map1);
					if (list1.size() > 0) {

					}
					produceInfo.getBlock("result").setCell(j, "fdKeyId", fdProduceId);
					produceInfo.getBlock("result").setCell(j, "fdTitle", fdPlanYear + "年" + fdPlanMonth + "月的生产计划");
					produceInfo.getBlock("result").setCell(j, "fdMiningId",
							UserSession.getInSessionProperty("miningCode"));
					produceInfo.getBlock("result").setCell(j, "fdMiningName",
							UserSession.getInSessionProperty("miningName"));
					produceInfo.getBlock("result").setCell(j, "fdPlanMonth", fdPlanMonth);
					produceInfo.getBlock("result").setCell(j, "fdPlanYear", fdPlanYear);
					produceInfo.getBlock("result").setCell(j, "fdPlanMonth", fdPlanMonth);
					produceInfo.getBlock("result").setCell(j, "fdPlanMine", 0);
					produceInfo.getBlock("result").setCell(j, "fdPlanGrub", 0);
					produceInfo.getBlock("result").setCell(j, "fdPlanDeploitation", 0);
					produceInfo.getBlock("result").setCell(j, "fdPlanProjectNum", 0);
					produceInfo.getBlock("result").setCell(j, "fdMineUnit", "吨");
					produceInfo.getBlock("result").setCell(j, "fdGrubUnit", "米");
					produceInfo.getBlock("result").setCell(j, "fdDeploitationUnit", "米");
					produceInfo.getBlock("result").setCell(j, "fdPlanStatus", "草拟");
					produceInfo.getBlock("result").setCell(j, "fdPlanSource", "1");
					produceInfo.getBlock("result").setCell(j, "fdCreateDepartmentId",
							UserSession.getInSessionProperty("orgCode"));
					produceInfo.getBlock("result").setCell(j, "fdCreateDepartment",
							UserSession.getInSessionProperty("orgName"));
					produceInfo.getBlock("result").setCell(j, "fdEditor",
							UserSession.getInSessionProperty("UserCname"));
					produceInfo.getBlock("result").setCell(j, "fdEditDate", StaticTools.getDate());
					j++;
				} else {
					if ("草拟".equals(_result.get(0).getFdPlanStatus())) {
						fdProduceId = _result.get(0).getFdKeyId();
						if (_result.get(0).getFdPlanSource().equals("0")) {
							produceInfo.getBlock("updateResult").setCell(h, "fdKeyId", fdProduceId);
							h++;
						}
					} else {
						inInfo.setStatus(-1);
						inInfo.setMsg("已经存在有效的该年月生产计划，不能再上报了！");
						return inInfo;
					}
				}
				// 添加生产计划主键
				inInfo.getBlock("result").setCell(i, "fdProduceplanId", fdProduceId);
			} else {
				inInfo.setStatus(-1);
				inInfo.setMsg("上报的专业计划中有的没有明细，请添加明细后上报！");
				return inInfo;
			}
			// 上报时要记录发布人
			inInfo.getBlock("result").setCell(i, "fdPulisherId", UserSession.getUserId());
			inInfo.getBlock("result").setCell(i, "fdPulisher", UserSession.getInSessionProperty("UserCname"));
		}
		super.update(inInfo, "ZMCPProfessionalPlan.report");// 更改专业计划主表状态为上报
		super.insert(produceInfo, "ZMCPProducePlan.insert");// 若没有生产计划主表，新增一条
		super.update(produceInfo, "ZMCPProducePlan.updatePlanSource", "updateResult");// 若存在生产计划主表，并且计划来源为0-"新增"，则更改计划来源为2-"混合"
		super.update(inInfo, "ZMCPProducePlanZC.updateProduceplanId");// 回填明细表的生产计划ID
		super.update(inInfo, "ZMCPProducePlanZK.updateProduceplanId");// 回填明细表的生产计划ID
		super.update(inInfo, "ZMCPProducePlanGLAZ.updateProduceplanId");// 回填明细表的生产计划ID
		super.update(inInfo, "ZMCPProducePlanZC.updateProduceplanCC");
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}

	/**
	 * <p>
	 * Function: validate
	 * </p>
	 * <p>
	 * Description: 对主表记录新增，删除，上报前的后台校验
	 * </p>
	 */
	@SuppressWarnings("rawtypes")
	private boolean validate(ZMCPProfessionalPlan _dom, String sqlName, Map<String, String> params, String type) {
		if (params == null) {
			params = new HashMap<String, String>();
		}
		List result = dao.query(sqlName, params);
		if (result != null && result.size() > 0 && !"report".equals(type)) {
			return true;
		}
		return false;
	}

	@SuppressWarnings("rawtypes")
	private boolean validate1(EiInfo inInfo, String sqlName, Map<String, String> params, String type) {
		if (params == null) {
			params = new HashMap<String, String>();
		}
		List result = dao.query(sqlName, params);
		if (result != null && result.size() > 0 && !"report".equals(type)) {
			if (type.equals("insert")) {
				String msg = "该年月的抽采专业计划已经存在！";
				inInfo.setMsg(msg);
			} else if (type.equals("delete")) {
				String msg = "该抽采专业包含详细计划，不可删除！";
				inInfo.setMsg(msg);
			} else if (type.equals("update")) {
				String msg = "该抽采专业包含详细计划，不可修改！";
				inInfo.setMsg(msg);
			}
			inInfo.setStatus(-1);
			return true;
		}
		return false;
	}

	/**
	 * 钻孔类别下拉框
	 * <p>
	 * Function: initValidateMonthCombobox
	 * </p>
	 */
	private EiBlock getHoleType() {
		EiBlock block = new EiBlock("cmes.cp.holeType");

		EiColumn column = new EiColumn("value");
		block.addMeta(column);
		column = new EiColumn("display");
		block.addMeta(column);

		block.addRows(InitialData.getInstance().getEiBlockByNo("cmes.cp.holeType").getRows());
		block.addRows(InitialData.getInstance().getEiBlockByNo("cmes.cp.holeType2").getRows());
		return block;
	}
}
